<template>
  <div class="xtx-city">
    <div class="select" @click="toggleDialog" >
      <span class="placeholder" v-if="!changeResult.provinceName">请选择配送地址</span>
      <span class="value" v-else>
        {{changeResult.provinceName}} {{changeResult.cityName}} {{changeResult.countyName}}
      </span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="showFlag">
      <span
      class="ellipsis"
      v-for="i in cityData"
      :key="i"
      @click="cityHandler(i)"
      >{{i.name}}</span>
    </div>
  </div>
</template>
<script>
import { onMounted, ref, reactive, watch } from 'vue'
import axios from 'axios'
export default {
  name: 'XtxCity',
  setup () {
    // 控制展开收起,默认收起
    const showFlag = ref(false)
    const cityData = ref({})
    let cacheList = [] // 缓存最初的城市列表 用作复原
    function toggleDialog () {
      showFlag.value = !showFlag.value
    }
    // 获取城市数据
    function loadCityList () {
      axios({
        url: 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json',
        method: 'GET'
      }).then((res) => {
        console.log(res, 'city')
        cityData.value = res.data
        // 缓存一份最初的list 每次关闭之后再打开 就渲染最初的省市区
        cacheList = res.data
      })
    }
    // 点击替换城市
    function cityHandler (i) {
      cityData.value = i.areaList
      if (i.level === 0) {
        changeResult.provinceCode = i.code
        changeResult.provinceName = i.name
      }
      if (i.level === 1) {
        changeResult.cityCode = i.code
        changeResult.cityName = i.name
      }
      // 当前点击的区
      if (i.level === 2) {
        changeResult.countyCode = i.code
        changeResult.countyName = i.name
        showFlag.value = false
      }
    }
    // 选完之后 弹框关闭 把所有选过的额显示在视图里
    const changeResult = reactive({
      provinceCode: '', // 省code
      provinceName: '', // 省名称
      cityCode: '', // 城市code
      cityName: '', // 城市名称
      countyCode: '', // 地区code
      countyName: '' // 地区名
    })

    // 监听弹框的打开状态 打开之后 把缓存的list重新赋值给

    watch(() => {
      return showFlag.value
    }, () => {
      if (showFlag.value) { cityData.value = cacheList }
    })
    onMounted(() => {
      loadCityList()
    })
    return {
      showFlag,
      toggleDialog,
      cityData,
      cityHandler,
      changeResult,
      cacheList
    }
  }
}

</script>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  margin-left: 10px;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
